﻿//对JQ进行扩展,包括延时执行方法,循环执行,及把一个对象转换为JSON字符串
jQuery.extend(
{
    toJSON: function (o)
    {
        //#region
        var useHasOwn = !!{}.hasOwnProperty,
        isNative = false,
        pad = function (n) { return n < 10 ? "0" + n : n },
        toString = Object.prototype.toString,
        isDefined = function (v) { return typeof v !== 'undefined' },
        isArray = function (v) { return toString.apply(v) === '[object Array]' },
        isDate = function (v) { return toString.apply(v) === '[object Date]' },
        isString = function (v) { return typeof v === 'string' },
        isBoolean = function (v) { return typeof v === 'boolean' },
        m = { "\b": '\\b', "\t": '\\t', "\n": '\\n', "\f": '\\f', "\r": '\\r', '"': '\\"', "\\": '\\\\' },
        encodeString = function (s) { if (/["\\\x00-\x1f]/.test(s)) { return '"' + s.replace(/([\x00-\x1f\\"])/g, function (a, b) { var c = m[b]; if (c) { return c } c = b.charCodeAt(); return "\\u00" + Math.floor(c / 16).toString(16) + (c % 16).toString(16) }) + '"' } return '"' + s + '"' },
        encodeArray = function (o) { var a = ["["], b, i, l = o.length, v; for (i = 0; i < l; i += 1) { v = o[i]; switch (typeof v) { case "undefined": case "function": case "unknown": break; default: if (b) { a.push(',') } a.push(v === null ? "null" : jQuery.toJSON(v)); b = true } } a.push("]"); return a.join("") };
        encodeDate = function (o) { return '"' + o.getFullYear() + "-" + pad(o.getMonth() + 1) + "-" + pad(o.getDate()) + "T" + pad(o.getHours()) + ":" + pad(o.getMinutes()) + ":" + pad(o.getSeconds()) + '"' };
        doEncode = function (o)
        {
            if (!isDefined(o) || o === null) { return "null" }
            else if (isArray(o)) { return encodeArray(o) }
            else if (isDate(o)) { return encodeDate(o) }
            else if (isString(o)) { return encodeString(o) }
            else if (typeof o == "number") { return isFinite(o) ? String(o) : "null" }
            else if (isBoolean(o)) { return String(o) }
            else
            {
                var a = ["{"], b, i, v;
                if (!o.getElementsByTagName)
                {
                    for (i in o)
                    {
                        if (!useHasOwn || o.hasOwnProperty(i))
                        {
                            v = o[i];
                            switch (typeof v)
                            {
                                case "undefined":
                                case "function":
                                case "unknown":
                                    break;
                                default:
                                    if (b)
                                    {
                                        a.push(',')
                                    }
                                    a.push(doEncode(i), ":", v === null ? "null" : doEncode(v));
                                    b = true
                            }
                        }
                    }
                }
                a.push("}");
                return a.join("")
            }
        }
        return doEncode(o);
        //#endregion
    },
    format: function (formater, arg)
    {
        //#region
        var result = formater || "", args = arguments;
        if (arguments.length > 1)
        {
            if (/\{(\d+)\}/g.test(formater))
            {
                result = result.replace(/\{(\d+)\}/g, function (m, i)
                {
                    return args[parseInt(i) + 1] || m;
                });
            }
            else if (args.length == 2 && typeof args[1] == "object")
            {
                result = result.replace(/\{(\w+)\}/g, function (m, i)
                {
                    return args[1][i] || m;
                });
            }
        }
        return result;
        //#endregion
    }
});